---@class GT.stateUi 状态面板
---@field selectUnit table<RoleID,GT.unit> 玩家选择的单位
---@field tickAttr table<RoleID,Str> 属性面板逻辑帧
GT.stateUi = class()



function GT.stateUi:ctor()
    self.prefab = {}
    self.ui = {}
    ---刷新状态面板数据 逻辑帧
    self.tickId = {}
    self.tickAttr = {}
    self.selectUnit = {}
    self.prefab["状态面板"] = GT.ui.prefab.obj({
        module = "1972a8f8-4c25-415c-a767-42b4714243ee",
        parent = "panel_2",
    })
    local p = self.prefab["状态面板"]

    self.ui["self"] = p:child(GT.ui, "状态面板")
    self.ui["self"].binding.x=955.0
    self.ui["self"].binding.y=125

    ---血条
    self.ui["血条"] = p:child(GT.ui.control.progress, "血条")
    self.ui["血条"].binding.bind_attr = {
        max = UnitKeyFloatAttr['最大生命值'],
        cur = UnitKeyFloatAttr['当前生命值'],
    }

    self.ui["当前血"] = p:child(GT.ui.control.label, "血条.当前血")
    self.ui["当前血"].binding.attr = UnitKeyFloatAttr['当前生命值']

    self.ui["最大血"] = p:child(GT.ui.control.label, "血条.最大血")
    self.ui["最大血"].binding.attr = UnitKeyFloatAttr['最大生命值']


    ---蓝条
    self.ui["蓝条"] = p:child(GT.ui.control.progress, "蓝条")
    self.ui["蓝条"].binding.bind_attr = {
        max = UnitKeyFloatAttr['最大法力值'],
        cur = UnitKeyFloatAttr['当前法力值'],
    }

    self.ui["当前蓝"] = p:child(GT.ui.control.label, "蓝条.当前蓝")
    self.ui["当前蓝"].binding.attr = UnitKeyFloatAttr['当前法力值']

    self.ui["最大蓝"] = p:child(GT.ui.control.label, "蓝条.最大蓝")
    self.ui["最大蓝"].binding.attr = UnitKeyFloatAttr['最大法力值']

    ---经验
    self.ui["经验条"] = p:child(GT.ui.control.progress, "经验条")
    self.ui["经验"] = p:child(GT.ui.control.label, "经验条.经验")


    self.ui["单位昵称"] = p:child(GT.ui.control.label, "单位昵称")
    self.ui["特殊内容"] = p:child(GT.ui.control.label, "特殊内容")

    ---攻击
    self.ui["攻击"] = p:child(GT.ui.control.label, "攻击.攻击.攻击")
    self.ui["攻击"].binding.attr = UnitKeyFloatAttr['物理攻击']

    self.ui["法术攻击"] = p:child(GT.ui.control.label, "法术攻击.法术攻击.法术攻击")
    self.ui["法术攻击"].binding.attr = UnitKeyFloatAttr['法术攻击']

    ---攻击
    self.ui["防御"] = p:child(GT.ui.control.label, "防御.防御.防御")
    self.ui["防御"].binding.attr = UnitKeyFloatAttr['物理防御']

    self.ui["法术防御"] = p:child(GT.ui.control.label, "法术防御.法术防御.法术防御")
    self.ui["法术防御"].binding.attr = UnitKeyFloatAttr['法术防御']

    ---等级
    self.ui["等级"] = p:child(GT.ui.control.label, "等级底板.等级")
    self.ui["等级底板"] = p:child(GT.ui.control.image, "等级底板")

    ---属性展示
    self.ui["属性显示框"] = p:child(GT.ui.control.scroll_view, "属性显示框")
    self.ui["属性显示框"].binding.z_order=9999
    self.ui["属性内容"] = p:child(GT.ui.control.label, "属性显示框.属性内容")

    ---属性展示框触发按钮
    self.ui["属性信息"] = p:child(GT.ui.control.button, "侧边栏底板.属性信息")
    self.ui["属性信息"]:enter(function(data, player)
        self:set_Tip(player, self.selectUnit[player.pid])
    end)
    self.ui["属性信息"]:leave(function(data, player)
        self.ui["属性显示框"].u[player.pid].binding.show = false
    end)
end

---设置状态内容
---@param p GT.player
---@param u GT.unit
function GT.stateUi:set(p, u)
    self.ui["单位昵称"].u[p.pid].binding.text = u:get_name()

    self.selectUnit[p.pid] = u
    if self.tickId[p.pid] == nil then
        self.tickId[p.pid] = tick.add(function(uuid)
            local u = self.selectUnit[p.pid]
            if u:api_is_destroyed() then
                tick.del(uuid)
                self.tickId[p.pid]=nil
                self.ui["单位昵称"].u[p.pid].binding.text='单位昵称'
                self.ui["等级"].u[p.pid].binding.text = 0
                self.ui["经验条"].u[p.pid].binding.max_value = 0
                self.ui["经验条"].u[p.pid].binding.bar = {
                    progress = 0,
                    time = 0,
                }
                self.ui["经验"].u[p.pid].binding.text ='0/0'
                return
            end
            
            if u:get_type()==UnitCategory['建筑']then
                self.ui["等级"].u[p.pid].binding.text = u:diy_get_build_lv()
            else
                self.ui["等级"].u[p.pid].binding.text = u:api_get_level()
            end
            self.ui["经验条"].u[p.pid].binding.max_value = u:api_get_upgrade_exp()
            self.ui["经验条"].u[p.pid].binding.bar = {
                progress = u:api_get_exp(),
                time = 0,
            }
            self.ui["经验"].u[p.pid].binding.text = tostring(u:api_get_exp()) .. "/" ..
            tostring(u:api_get_upgrade_exp())
        end)
    end
end

---设置提示属性内容
---@param p GT.player
---@param u GT.unit
function GT.stateUi:set_Tip(p, u)
    if u==nil then
        return
    end
    local t = {}
    for key, value in pairs(UnitKeyFloatAttr) do
        local attr=u:api_get_str_attr(value)
        if attr~=nil and attr~="" then
            table.insert(t, key .. ":" ..attr )
        end
        
    end

    self.ui["属性内容"].u[p.pid].binding.text = table.concat(t, "\n")
    self.ui["属性显示框"].u[p.pid].binding.show = true
end
